import { ElMessage } from 'element-plus'
import { Result } from '@/utils/Http'
import router from '@/router'

/**
 * 后台返回错误码定义
 */
export enum ErrorCode{
  ERR_500 = '500',
  ERR_600 = '600',
  OK_200 = '200',
  OK_201 = '201',
}

/**
 * 错误码白名单
 */
export const ErrorCodeIgnore:Array<string> = [
  '214'
]

/**
 * 接口异常处理
 */
export const handleError = (result:Result) => {
  if(ErrorCodeIgnore.includes(result.code)){
    return
  }

  if(result.code == ErrorCode.ERR_600) {
    showErrorMsg(result)
    router.push('/login')
    return
  }

  if(result.code != ErrorCode.OK_200 && result.code != ErrorCode.OK_201){
    showErrorMsg(result)
    return
  }
}

function showErrorMsg(result: Result){
  ElMessage({
    showClose: true,
    message: result.msg ? result.msg : '系统异常',
    type: 'error',
  })
}